package com.example.neusoft.yuejian.Fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.neusoft.yuejian.Activity.ExerciseDetailActivity;
import com.example.neusoft.yuejian.Base.BaseFragment;
import com.example.neusoft.yuejian.Base.BaseListListener;
import com.example.neusoft.yuejian.Base.BaseListener;
import com.example.neusoft.yuejian.Base.BaseModel;
import com.example.neusoft.yuejian.Bean.exercisesbean;
import com.example.neusoft.yuejian.R;
import com.example.neusoft.yuejian.Util.MyDividerItemDecoration;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.List;

import retrofit2.Call;

public class SonHistFragment extends BaseFragment {
    private List<exercisesbean> list;
    private RecyclerView rv_exer;
    private static final int EMPTY_VIEW = 1;
    private int i;


    @Override
    public int getLayoutFile() {
        return R.layout.fragment_layout_exercise;
    }

    @Override
    public void initView() {
        rv_exer=view.findViewById(R.id.rv_exer);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        rv_exer.setLayoutManager(linearLayoutManager);
        rv_exer.setHasFixedSize(true);
        rv_exer.addItemDecoration(new MyDividerItemDecoration(getActivity(),LinearLayoutManager.VERTICAL));
        i=getArguments().getInt("key",0);
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        getData(i);
    }

    public static SonHistFragment newInstance(String tabTitle) {
        Bundle args = new Bundle();
        SonHistFragment fragment = new SonHistFragment();
        args.putString("title", tabTitle);
        fragment.setArguments(args);
        return fragment;
    }

    public void  getData(int level){
        BaseModel<exercisesbean> model=new BaseModel<>();
        Call<List<exercisesbean>> call=model.service.getexercisesbean(level);
        model.callListEnqueue(call,listener);
    }

    BaseListener<List<exercisesbean>> listener=new BaseListener<List<exercisesbean>>() {
        @Override
        public void onResponse(List<exercisesbean> exercisesbeans) {
               list=exercisesbeans;
               rv_exer.setAdapter(new XunLianAdapter(list));
        }

        @Override
        public void onFail(String message) {

        }
    };


    class XunLianAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        private List<exercisesbean> list;
        View view;

        public XunLianAdapter(List<exercisesbean> list){
            this.list=list;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater=LayoutInflater.from(getActivity());
            if(viewType==EMPTY_VIEW) {
                view=inflater.inflate(R.layout.rv_nosecond,parent,false);
                return new EmptoryHolder(view);
            }
            else {
                view=inflater.inflate(R.layout.rv_exer,parent,false);
                return new ViewHolder(view);
            }

        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
                if(holder instanceof ViewHolder) {
                    ViewHolder viewHolder=(ViewHolder)holder;
                    Picasso.with(getActivity()).load("http://169.254.192.205/YueJian/public/static/img/exerimg/"+list.get(position).getExerpic()).into(viewHolder.img_xl);
                    viewHolder.tx_name.setText(list.get(position).getExername());
                    viewHolder.fr.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getActivity(), ExerciseDetailActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("exerlist",(Serializable) list.get(position));
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });



                }
        }

        @Override
        public int getItemCount() {
            return list.size() > 0 ? list.size() : 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView img_xl;
        private TextView tx_name;
        private FrameLayout fr;
        public ViewHolder(View itemView) {
            super(itemView);
            img_xl=(ImageView)itemView.findViewById(R.id.img_xl);
            tx_name=(TextView)itemView.findViewById(R.id.tx_name);
            fr=(FrameLayout)itemView.findViewById(R.id.fr);
            tx_name.setBackgroundColor(Color.argb(255,0,255,0));
        }
    }

    class  EmptoryHolder extends RecyclerView.ViewHolder{
        private  ImageView imageView;

        private TextView textView;
        public EmptoryHolder(View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.iv_emtry);
            textView=(TextView)itemView.findViewById(R.id.tv_emtry);

        }
    }

}
